package com.smallrig.mall.template;

import cn.hutool.extra.spring.SpringUtil;
import com.smallrig.extension.dto.BizScene;
import com.smallrig.extension.ext.ExtensionExecutor;
import com.smallrig.extension.statemachine.StateMachine;
import com.smallrig.mall.template.entity.Order;
import com.smallrig.mall.template.entity.Product;
import com.smallrig.mall.template.event.TestEvent;
import com.smallrig.mall.template.exchange.TestExchange;
import com.smallrig.mall.template.extension.ExtV1;
import com.smallrig.mall.template.extension.ExtVersion;
import com.smallrig.mall.template.http.EnableExchangeClients;
import com.smallrig.mall.template.liteflow.YourClass;
import com.smallrig.mall.template.mychain.OrderChain;
import com.smallrig.mall.template.mychain.OrderCtx;
import com.smallrig.mall.template.plugin.BeanListFactoryBean;
import com.smallrig.mall.template.plugin.Vehicle;
import com.smallrig.mall.template.service.TranService;
import com.smallrig.mall.template.state.OrderStatus;
import com.smallrig.mall.template.state.OrderStatusChangeEvent;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.plugin.core.PluginRegistry;
import org.springframework.plugin.core.config.EnablePluginRegistries;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@EnableExchangeClients
@MapperScan("com.smallrig.mall.template.mapper")
@SpringBootApplication//(exclude = DataSourceAutoConfiguration.class)
@EnableScheduling
@EnableAspectJAutoProxy(exposeProxy = true)
@EnablePluginRegistries(Vehicle.class)
//true强制走cglib代理，false并且目标类实现了接口走jdk打理
@EnableAsync(proxyTargetClass = true)
//@EnableAdminServer // 添加此行代码

public class TemplateApplication implements CommandLineRunner {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(TemplateApplication.class, args);
        TranService tranService = context.getBean(TranService.class);
        tranService.testTran();
//        OrderSubmitChain orderSubmitChain = context.getBean(OrderSubmitChain.class);
//        OrderContext orderContext = new OrderContext(1L,"2",3);
//        orderSubmitChain.execute(orderContext);
    }

    @Resource
    private TestExchange testExchange;

    @Autowired
    @Qualifier("vehicleRegistry")
    PluginRegistry<Vehicle, String> vehicleRegistry;


    @Autowired
    private BeanListFactoryBean beanListFactoryBean;

    @Resource
    private ExtensionExecutor extensionExecutor;

    @Resource
    private ApplicationEventPublisher applicationEventPublisher;


    @Resource
    private YourClass yourClass;

    @Resource
    private StateMachine<OrderStatus, OrderStatusChangeEvent, Order> colaOrderStateMachine;



    @Override
    public void run(String... args) throws Exception {
//        testExchange.test(new Product());
//        testExchange.test2("1",1L);
//        testExchange.test3("1");

        colaOrderStateMachine.fireEvent(OrderStatus.WAIT_PAYMENT, OrderStatusChangeEvent.PAYED, null);
        colaOrderStateMachine.fireEvent(OrderStatus.WAIT_DELIVER, OrderStatusChangeEvent.DELIVERY, null);
        colaOrderStateMachine.fireEvent(OrderStatus.WAIT_RECEIVE, OrderStatusChangeEvent.RECEIVED, null);

        colaOrderStateMachine.fireEvent(OrderStatus.WAIT_PAYMENT, OrderStatusChangeEvent.UPDATE, null);
        colaOrderStateMachine.fireEvent(OrderStatus.WAIT_DELIVER, OrderStatusChangeEvent.UPDATE, null);
        colaOrderStateMachine.fireEvent(OrderStatus.WAIT_RECEIVE, OrderStatusChangeEvent.UPDATE, null);

        yourClass.testConfig();

        testExchange.test4(new String[]{"1","2"});
        testExchange.test5(Arrays.asList(1,2,3));


        beanListFactoryBean.getObject();
        List<Vehicle> vehicles = vehicleRegistry.getPlugins();
        for (Vehicle vehicle : vehicles) {
            vehicle.reachGoal("1");
        }

        String lry = extensionExecutor.execute(ExtVersion.class, BizScene.valueOf(1), v -> v.sayHello("lry"));
        System.out.println(lry);

        ExtV1 extVersion = (ExtV1) extensionExecutor.load(ExtVersion.class, BizScene.valueOf("1"));
        System.out.println(extVersion);

        OrderCtx orderContext = new OrderCtx(1L,"2",3);
        extensionExecutor.execute(OrderChain.class,orderContext);

        System.out.println(extensionExecutor.load(OrderChain.class));
        System.out.println(extensionExecutor.load(ExtVersion.class));

        applicationEventPublisher.publishEvent(new TestEvent("dsaf"));
    }
}
